﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WatiN.Core;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class ElementCommand : CommandBase
    {
        public ElementCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }
        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            object element=null;
            object selectList = null;
            object checkBox = null;
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = parameter[1];
            string idOrNameOrClassOrValue = "";
            testLineResult.LineSubCommand = command;
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            try
            {
                
                switch(command.ToLower())
                {
                    case "exists":
                    case "exist":
                        idOrNameOrClassOrValue = parameter[1].Replace("\r\n", "").Trim();
                        element = GetElement(idOrNameOrClassOrValue);
                        if (element == null)
                        {
                            testLineResult.LineMessage = "Could not find element :" + parameter[1];
                            testLineResult.LinePassed = false;
                        }
                        else
                        {
                            testLineResult.LinePassed = true;
                        }
                        break;
                    
                    case "click":
                        idOrNameOrClassOrValue = parameter[1].Replace("\r\n","").Trim();
                        element = GetElement(idOrNameOrClassOrValue);
                        WebBrowserObject.ElementProperties tempelement = (WebBrowserObject.ElementProperties)element;
                        if (!tempelement.Exists)
                        {
                            testLineResult.LineMessage = "Could not find element :" + parameter[1];
                            testLineResult.LinePassed = false;
                        }else
                        {
                            ElementClick(tempelement.Element);
                            testLineResult.LinePassed = true;
                        }
                        break;

                    case "selectvalue":
                        var selectListId = parameter[1].Split(':')[0];
                        var selectListValue = parameter[1].Split(':')[1].Replace("\r\n","");
                        selectList = GetSelectElement(selectListId);
                        WebBrowserObject.ElementProperties tempSelect = (WebBrowserObject.ElementProperties) selectList;
                        if (!tempSelect.Exists)
                        {
                            testLineResult.LineMessage = "Could not find select List :" + selectListId;
                            testLineResult.LinePassed = false;
                        }
                        else
                        {
                            testLineResult.LinePassed = ElementDropDownListSelectValue(selectList, selectListValue);


                        }
                        break;
                    case "checkboxvalue":
                        var checkBoxValue = parameter[1].Split(':')[0];
                        string[] chkBoxArray = parameter[1].Split(':');
                        var checkBoxValueSet = parameter[1].Split(':')[1].Replace("\r\n", "");
                        checkBox = GetCheckBoxElement(chkBoxArray[0].Replace("\r\n", ""), "");
                        WebBrowserObject.ElementProperties tempChckBoxValue = (WebBrowserObject.ElementProperties)checkBox;
                        if (!tempChckBoxValue.Exists)
                        {
                            testLineResult.LineMessage = "Could not find check Box with value :" + checkBoxValue;
                            testLineResult.LinePassed = false;
                        }
                        else
                        {
                            CheckBoxCheck(checkBox, checkBoxValueSet);
                            testLineResult.LinePassed = true;
                        }
                        break;
                    case "checkboxid":
                        var checkBoxid = parameter[1].Split(':')[0];
                        var checkBoxValueIDSet = parameter[1].Split(':')[1].Replace("\r\n", "");
                        checkBox = GetCheckBoxElement("", checkBoxid);
                        WebBrowserObject.ElementProperties tempChckBoxID = (WebBrowserObject.ElementProperties)checkBox;
                        if (!tempChckBoxID.Exists)
                        {
                            testLineResult.LineMessage = "Could not find check Box with id :" + checkBoxid;
                            testLineResult.LinePassed = false;
                        }
                        else
                        {
                            CheckBoxCheck(checkBox, checkBoxValueIDSet);
                            testLineResult.LinePassed = true;
                        }
                        break;

                    case "radiovalue":
                        var radioid = parameter[1].Split(':')[0];
                        var radioidSet = parameter[1].Split(':')[1].Replace("\r\n", "");
                        checkBox = GetRadioElement(radioidSet);
                        WebBrowserObject.ElementProperties tempRadio = (WebBrowserObject.ElementProperties)checkBox;
                        if (!tempRadio.Exists)
                        {
                            testLineResult.LineMessage = "Could not find radio button with value :" + radioidSet;
                            testLineResult.LinePassed = false;
                        }
                        else
                        {
                            RadioButtonCheck(checkBox, radioidSet);
                            testLineResult.LinePassed = true;
                        }
                        break;
                }

                
                
                
                

            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message+"." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }

            return testLineResult.LinePassed;
        }


        public bool ElementExist(object element)
        {
            bool returnValue = false;

            return this.BrowserObject.ElementExist(element);

        }

        public void ElementClick(object element)
        {
            this.BrowserObject.ElementClick(element);

        }

        public bool ElementDropDownListSelectValue(object selectList, string selectListValue)
        {
            WebBrowserObject.ElementProperties elem = (WebBrowserObject.ElementProperties) selectList;
            return BrowserObject.SelectListSelect(elem.Element, selectListValue);
        }

        public void CheckBoxCheck(object checkBox,string valueOrId)
        {
            WebBrowserObject.ElementProperties elem = (WebBrowserObject.ElementProperties)checkBox;
            this.BrowserObject.CheckBoxCheck(elem.Element, valueOrId);

        }

        public void RadioButtonCheck(object radioButton, string valueOrId)
        {
            WebBrowserObject.ElementProperties elem = (WebBrowserObject.ElementProperties)radioButton;
            this.BrowserObject.RadioButtonCheck(elem.Element, valueOrId);

        }

        public WebBrowserObject.ElementProperties GetRadioElement(string radioValue)
        {
            WebBrowserObject.ElementProperties radio = new WebBrowserObject.ElementProperties();
            radio = this.BrowserObject.RadioButton(Find.ByValue(radioValue));
            if (!radio.Exists)
            {
                radio = this.BrowserObject.CheckBox(Find.ById(radioValue));
            }
            if (!radio.Exists)
            {
                radio.Exists = false;
            }

            return radio;
        }


        public WebBrowserObject.ElementProperties GetCheckBoxElement(string checkBoxValue, string checkBoxId = "")
        {
            WebBrowserObject.ElementProperties checkBox = new WebBrowserObject.ElementProperties();
            checkBox = this.BrowserObject.CheckBox(Find.ByValue(checkBoxValue));
            if (!string.IsNullOrEmpty(checkBoxId))
            {
                if (!checkBox.Exists)
                {
                    checkBox = this.BrowserObject.CheckBox(Find.ById(checkBoxId));
                }
            }
            if (!checkBox.Exists)
            {
                checkBox.Exists = false;
            }

            return checkBox;
        }

        public WebBrowserObject.ElementProperties GetSelectElement(string selectListId)
        {
            WebBrowserObject.ElementProperties selectList = new WebBrowserObject.ElementProperties();
            selectList = this.BrowserObject.SelectList(Find.ById(selectListId));
            
            if (!selectList.Exists)
            {
                selectList = this.BrowserObject.SelectList(Find.ByName(selectListId));
            }
            if (!selectList.Exists)
            {
                selectList = this.BrowserObject.SelectList(Find.ByClass(selectListId));
            }
            if (!selectList.Exists)
            {
                
                selectList = _getElementFromIFrame(this.BrowserObject, selectListId);
                
            }
            if (!selectList.Exists)
            {
                selectList.Exists = false;
            }

            return selectList;
        }

        public object GetElement(string idOrNameOrClassOrValue)
        {
            WebBrowserObject.ElementProperties element=new WebBrowserObject.ElementProperties();
            try
            {
                element = this.BrowserObject.Element(Find.ById(idOrNameOrClassOrValue));
                if (!element.Exists)
                {
                    element = this.BrowserObject.Element(Find.ByName(idOrNameOrClassOrValue));
                }
                if (!element.Exists)
                {
                    element = this.BrowserObject.Element(Find.ByValue(idOrNameOrClassOrValue));
                }
                if (!element.Exists)
                {
                    element = this.BrowserObject.Element(Find.ByClass(idOrNameOrClassOrValue));
                }
                if (!element.Exists)
                {
                    element = _getElementFromIFrame(this.BrowserObject, idOrNameOrClassOrValue);
                }
               
            }
            catch (UnauthorizedAccessException ex) { }
            if (!element.Exists)
            {
                element = _getElementFromIFrame(this.BrowserObject, idOrNameOrClassOrValue);
            }
            return element;
        }

        private WebBrowserObject.ElementProperties _getElementFromIFrame(WebBrowserObject browserObject, string idOrNameOrClassOrValue)
        {
            WebBrowserObject.ElementProperties element = new WebBrowserObject.ElementProperties();
            try
            {
                if (browserObject.Frames().Count > 0)
                {
                 
                    for (int i = 0; i <= browserObject.Frames().Count() - 1; i++)
                    {
                        browserObject.WaitForCompleteIFrame(i, 5000);
                        element = browserObject.ElementIFrame(i, idOrNameOrClassOrValue);
                        if (element.Exists)
                        {
                            break;
                        }
                    }

                }
             
            }
            catch (UnauthorizedAccessException ex)
            {

            }
            return element;
        }
    }
}
